﻿using System;
using System.Collections.Generic;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Factory;
using Me.Prettyprint.Hector.Api.Query;
using Org.Apache.Cassandra.Thrift;

namespace Me.Prettyprint.Cassandra.Model.Thrift
{
    /// <summary>
    /// ThriftMultigetSliceCounterQuery is used to query for multiple counters
    /// in a single call. The query returns 0 or more rows.
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="N"></typeparam>
    public class ThriftMultigetSliceCounterQuery<K, N> : AbstractSliceQuery<K, N, long, CounterRows<K, N>>, MultigetSliceCounterQuery<K, N>
    {
        public ThriftMultigetSliceCounterQuery(Keyspace k, Serializer<K> keySerializer,
            Serializer<N> nameSerializer) : base(k, keySerializer, nameSerializer, LongSerializer.Instance)
        {
        }
        public List<K> Keys
        {
            get;
            set;
        }
        public MultigetSliceCounterQuery<K, N> SetKeys(List<K> keys)
        {
            Keys = keys;
            return this;
        }
        public override string ToString()
        {
            return "MultigetSliceQuery(" + Keys + "," + base.ToStringInternal() + ")";
        }
        public new MultigetSliceCounterQuery<K, N> SetColumnNames(List<N> columnNames)
        {
            return (MultigetSliceCounterQuery<K, N>) base.SetColumnNames(columnNames);
        }
        public new MultigetSliceCounterQuery<K, N> SetRange(N start, N finish, bool reversed, int count)
        {
            return (MultigetSliceCounterQuery<K, N>) base.SetRange(start, finish, reversed, count);
        }
        public new MultigetSliceCounterQuery<K, N> SetColumnFamily(string cf)
        {
            return (MultigetSliceCounterQuery<K, N>) base.SetColumnFamily(cf);
        }
        public override QueryResult<CounterRows<K, N>> Execute()
        {
            if (ColumnFamilyName == null || Keys == null)
            {
                throw new NullReferenceException("ColumnFamily name and Keys cannot be null");
            }
            Func<KeyspaceService, CounterRows<K, N>> x = (KeyspaceService ks) =>
            {
                List<byte[]> keylist = new List<byte[]>();
                foreach (K key in Keys)
                {
                    keylist.Add(KeySerializer.ToBytes(key));
                }
                ColumnParent columnParent = new ColumnParent(ColumnFamilyName);
                Dictionary<K, List<CounterColumn>> thriftRet = KeySerializer.FromBytesDictionary(
                    ks.MultigetCounterSlice(keylist, columnParent, GetPredicate()));
                return new CounterRowsImpl<K, N>(thriftRet, ColumnNameSerializer);
            };
            return new QueryResultImpl<CounterRows<K, N>>(
                Keyspace.DoExecute(new KeyspaceOperationCallbackBuilder<CounterRows<K, N>>(x)), this);
        }
    }
}
